package cn.zn.controller.system;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.dict.service.SysDictTypeService;
import cn.zn.dict.vo.SysDictTypeEditVo;
import cn.zn.dict.vo.SysDictTypePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典信息
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResultData list(SysDictTypePageVo vo) {
        Object list = dictTypeService.selectDictTypeList(vo);
        return ResultUtil.success(list);
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:info')")
    @GetMapping(value = "/{dictId}")
    public ResultData getInfo(@PathVariable String dictId) {
        return ResultUtil.success(dictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated SysDictTypeEditVo vo) {
        dictTypeService.insertDictType(vo);
        return ResultUtil.success();
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{dictId}")
    public ResultData edit(@PathVariable String dictId, @RequestBody @Validated SysDictTypeEditVo vo) {
        dictTypeService.updateDictType(dictId, vo);
        return ResultUtil.success();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public ResultData remove(@PathVariable String[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ResultUtil.success();
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public ResultData clearCache() {
        dictTypeService.clearDictCache();
        return ResultUtil.success();
    }

}
